package com.decorate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.decorate.Vos.DesignProductVo;
import com.decorate.Vos.ProductReqVo;
import com.decorate.Vos.ProductVo;
import com.decorate.Vos.R;
import com.decorate.mapper.ArchitectProductionMapper;
import com.decorate.mapper.DesignerMapper;
import com.decorate.pojo.ArchitectProduction;
import com.decorate.pojo.DesignProduction;
import com.decorate.pojo.Designer;
import com.decorate.pojo.SysDict;
import com.decorate.service.DesignProductionService;
import com.decorate.mapper.DesignProductionMapper;
import com.decorate.service.SysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author duji
* @description 针对表【design_production(设计作品)】的数据库操作Service实现
* @createDate 2022-08-24 22:12:41
*/
@Service
public class DesignProductionServiceImpl extends ServiceImpl<DesignProductionMapper, DesignProduction> implements DesignProductionService{

    //设计师表 Mapper
    @Autowired
    DesignerMapper designerMapper;

    //作品表 Mapper
    @Autowired
    DesignProductionMapper designProductionMapper;

    //作品-设计师中间表 Mapper
    @Autowired
    ArchitectProductionMapper architectProductionMapper;

    //字典表
    @Autowired
    public SysDictService sysDictService;

    @Override
    public R<List<DesignProductVo>> designProducts(Integer id) {
        //根据设计师id查询设计师类
        Designer designer = designerMapper.selectById(id);
        //根据设计师id 在 作品--设计师中间表 查询作品集合
        List<ArchitectProduction> architectProductions = architectProductionMapper.selectList(Wrappers.<ArchitectProduction>lambdaQuery().eq(ArchitectProduction::getArchitectId, designer.getId()).eq(ArchitectProduction::getIsDelete,"0"));
        List<DesignProductVo> productList = new ArrayList<>();
        for (ArchitectProduction architectProduction : architectProductions) {
            DesignProduction designProduction = designProductionMapper.selectById(architectProduction.getProductionId());
            DesignProductVo designProductVo = new DesignProductVo();
            designProductVo.setProductId(designProduction.getId());
            designProductVo.setDesignName(designer.getName());
            designProductVo.setIdentity(designer.getIdentity());
            designProductVo.setHouseType(designProduction.getHouseType());
            designProductVo.setArea(designProduction.getArea());
            designProductVo.setName(designProduction.getName());
            SysDict dict = sysDictService.getOne(Wrappers.<SysDict>lambdaQuery().eq(SysDict::getDictLabel, designProduction.getStyle()));
            designProductVo.setStyle(dict.getDictValue());
            productList.add(designProductVo);
        }
        return R.ok(productList);
    }

    @Override
    public R<List<DesignProductVo>> getProducts(ProductReqVo productReqVo) {
        LambdaQueryWrapper<DesignProduction> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DesignProduction::getIsDelete,"0");
        if(productReqVo.getIsAwards() == '1'){
            wrapper.eq(DesignProduction::getIsAwards,"1");
        }
        if(!productReqVo.getArea().isEmpty()){
            wrapper.eq(DesignProduction::getArea, productReqVo.getArea());
        }
        if(!productReqVo.getStyle().isEmpty()){
            SysDict dict = sysDictService.getOne(Wrappers.<SysDict>lambdaQuery().eq(SysDict::getDictValue, productReqVo.getStyle()));
            wrapper.eq(DesignProduction::getStyle, dict.getDictLabel());
        }
        if(!productReqVo.getHouseType().isEmpty()){
            SysDict dict = sysDictService.getOne(Wrappers.<SysDict>lambdaQuery().eq(SysDict::getDictValue, productReqVo.getHouseType()));
            wrapper.eq(DesignProduction::getHouseType, dict.getDictLabel());
        }
        List<DesignProduction> designProductions = designProductionMapper.selectList(wrapper);
        List<DesignProductVo> productVoList = new ArrayList<>();
        for (DesignProduction designProduction : designProductions) {
            DesignProductVo designProductVo = new DesignProductVo();
            designProductVo.setProductId(designProduction.getId());
            designProductVo.setName(designProduction.getName());
            designProductVo.setArea(designProduction.getArea());
            SysDict dict = sysDictService.getOne(Wrappers.<SysDict>lambdaQuery().eq(SysDict::getDictLabel,designProduction.getStyle()));
            designProductVo.setStyle(dict.getDictValue());
            designProductVo.setHouseType(designProduction.getHouseType());
            ArchitectProduction architectProduction = architectProductionMapper.selectOne(Wrappers.<ArchitectProduction>lambdaQuery().eq(ArchitectProduction::getProductionId, designProduction.getId()));
            Designer designer = designerMapper.selectById(architectProduction.getArchitectId());
            designProductVo.setDesignName(designer.getName());
            designProductVo.setIdentity(designer.getIdentity());
            productVoList.add(designProductVo);
        }
        return R.ok(productVoList);
    }

    @Override
    public R<ProductVo> getProduct(Integer id) {
        DesignProduction designProduction = designProductionMapper.selectById(id);
        ArchitectProduction architectProduction = architectProductionMapper.selectOne(Wrappers.<ArchitectProduction>lambdaQuery().eq(ArchitectProduction::getProductionId, id));
        Designer designer = designerMapper.selectById(architectProduction.getArchitectId());
        ProductVo productVo = new ProductVo();
        productVo.setProductId(designProduction.getId());
        productVo.setDesignName(designer.getName());
        productVo.setArea(designProduction.getArea());
        productVo.setHouseType(designProduction.getHouseType());
        productVo.setIdentity(designer.getIdentity());
        productVo.setName(designProduction.getName());
        productVo.setPrice(designProduction.getPrice());
        productVo.setDesignExplain(designProduction.getDesignExplain());
        productVo.setStyle(designProduction.getStyle());
        return R.ok(productVo);
    }
}
